package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.conmmon.CustomException;
import com.itheima.dao.SetMealDao;
import com.itheima.domain.DishFlavor;
import com.itheima.domain.Setmeal;
import com.itheima.domain.SetmealDish;
import com.itheima.dto.DishDto;
import com.itheima.dto.SetmealDto;
import com.itheima.service.SetMealService;
import com.itheima.service.SetmealDishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author liwanhu
 * @datetime 2021/10/25 21:24
 */
@Service
public class SetMealServiceImpl extends ServiceImpl<SetMealDao, Setmeal> implements SetMealService {
    @Autowired
    private SetmealDishService setmealDishService ;
    @Autowired
    private SetMealService setMealService;

    /**
     * 添加套餐
     * @param setmealDto
     */
    @Override
    public void saveGetDish(SetmealDto setmealDto) {
        //将指定的套餐信息保存到表中
        this.save(setmealDto);
        //获取套餐中的菜品数据
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes.stream().map((item)->{
            //将菜品对应的套餐名称插入表中
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());
        setmealDishService.saveBatch(setmealDishes);
    }

    /**
     * 实战第一天
     * 删除套餐,以及和套餐关联的菜品
     * @param ids
     */
    @Override
    @Transactional
    public void deleteWithDish(List<Long> ids) {

        //删除套餐关联的菜品数据
        //查询本次删除的套餐关联的菜品
        QueryWrapper<SetmealDish> qw = new QueryWrapper<>();
        qw.in("setmeal_id",ids);
        setmealDishService.remove(qw);

        //创建条件构造器 查询本次要删除的套餐状态是否是启售状态,列名:status
        QueryWrapper<Setmeal> queryWrapper = new QueryWrapper<>();
        //判断ids在表中是否存在
        queryWrapper.in("id",ids);
        //如果是启售状态不能被删除,如果不是启售状态则可以删除
        queryWrapper.eq("status",1);
        int count = this.count(queryWrapper);
        if (count > 0){
            throw new CustomException("套餐在启售状态中,不可被删除");
        }
        this.removeByIds(ids);
    }

    /**
     * 实战第一天
     *获取套餐中的菜品
     * @param id
     * @return
     */
    @Override
    public SetmealDto updateWithSetMeal(Long id) {
        Setmeal setmeal = this.getById(id);
        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(setmeal,setmealDto);
        QueryWrapper<SetmealDish> qw = new QueryWrapper<>();
        qw.eq("setmeal_id",id);
        List<SetmealDish> list = setmealDishService.list(qw);
        setmealDto.setSetmealDishes(list);
        return setmealDto;
    }

    /**
     * 实战第一天
     * 修改套餐信息
     * @param setmealDto
     */
    @Override
    @Transactional
    public void updateWithDish(SetmealDto setmealDto) {
        //获取当前需要修改的对象
        this.updateById(setmealDto);
        //创建条件构造器
        QueryWrapper<SetmealDish> qw = new QueryWrapper<>();
        qw.eq("setmeal_id",setmealDto.getId());
        //删除套餐菜品信息
        setmealDishService.remove(qw);

        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();

        List<SetmealDish> setmealDisheList = setmealDishes.stream().map((item)->{
            //将要修改的套餐id传入
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());
        setmealDishService.saveBatch(setmealDisheList);
    }
}
